<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="taskList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配送号" align="center" prop="taskId" />
      <el-table-column label="订单创建者" align="center" prop="nickName" />
      <el-table-column label="订单创建者手机号" align="center" prop="phone" />
      <el-table-column label="配送状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_task_status"
            :value="scope.row.taskStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="订单备注" align="center" prop="taskRecord" />
      <el-table-column label="配送的地点" align="center" prop="addressName" />
      <el-table-column label="订单的编号" align="center" prop="orderId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <template v-if="scope.row.taskStatus == 1">
            <el-button
              size="mini"
              type="text"
              @click="submitHandler(scope.row)"
              v-hasPermi="['system:task:expresssuccess']"
              >确认送达</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="failHandler(scope.row)"
              v-hasPermi="['system:task:expressfail']"
              >取消送达</el-button
            >
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配送任务对话框 -->
  </div>
</template>

<script>
import {
  listTask,
  getTask,
  delTask,
  addTask,
  updateTask,
  getMyTaskList,
  successArrive,
  failArrive,
} from "@/api/system/task";
import { getUserProfile } from "@/api/system/user";
export default {
  name: "Task",
  dicts: ["sys_task_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配送任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        createrId: null,
        expressId: null,
        taskStatus: null,
        addressId: null,
        orderId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    failHandler(data) {
      this.$prompt("请输入运送失败的备注信息", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          failArrive(data.taskId, value).then(() => {
            this.getList();
            this.$message({
              type: "success",
              message: "送达失败,返航请注意安全!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },

    /**
     * 确认送达
     */

    submitHandler(data) {
      this.$confirm("请确认已经送达致客户手中?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          successArrive(data.taskId).then(() => {
            this.getList();
            this.$message({
              type: "success",
              message: "送达成功,返航请注意安全!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    /** 查询配送任务列表 */
    getList() {
      this.loading = true;

      getUserProfile().then((item) => {
        getMyTaskList({ userId: item.data.userId }).then((response) => {
          this.taskList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        createrId: null,
        expressId: null,
        taskStatus: 0,
        taskRecord: null,
        addressId: null,
        createTime: null,
        updateTime: null,
        orderId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.taskId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配送任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const taskId = row.taskId || this.ids;
      getTask(taskId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配送任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.taskId != null) {
            updateTask(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids;
      this.$modal
        .confirm('是否确认删除配送任务编号为"' + taskIds + '"的数据项？')
        .then(function () {
          return delTask(taskIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/task/export",
        {
          ...this.queryParams,
        },
        `task_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
